namespace DotNetNuke.Common.Utilities
{
    using DotNetNuke.Common;
    using DotNetNuke.Security;
    using Microsoft.VisualBasic.CompilerServices;
    using System;
    using System.Configuration;
    using System.Xml;

    /// -----------------------------------------------------------------------------
    /// <summary>
    /// The Config class provides access to the web.config file
    /// </summary>
    /// <remarks>
    /// </remarks>
    /// <history>
    /// </history>
    /// -----------------------------------------------------------------------------
    public class Config
    {
        public static XmlDocument AddAppSetting(XmlDocument xmlDoc, string Key, string Value)
        {
            XmlNode xmlAppSettings = xmlDoc.SelectSingleNode("//appSettings");
            if (xmlAppSettings != null)
            {
                XmlElement xmlElement;
                XmlNode xmlNode = xmlAppSettings.SelectSingleNode("//add[@key='" + Key + "']");
                if (xmlNode != null)
                {
                    xmlElement = (XmlElement) xmlNode;
                    xmlElement.SetAttribute("value", Value);
                    return xmlDoc;
                }
                xmlElement = xmlDoc.CreateElement("add");
                xmlElement.SetAttribute("key", Key);
                xmlElement.SetAttribute("value", Value);
                xmlAppSettings.AppendChild(xmlElement);
            }
            return xmlDoc;
        }

        public static string GetConnectionString(string name)
        {
            string connectionString = "";
            if (name != "")
            {
                connectionString = ConfigurationManager.ConnectionStrings[name].ConnectionString;
            }
            if ((connectionString == "") && (name != ""))
            {
                connectionString = GetSetting(name);
            }
            return connectionString;
        }

        public static object GetSection(string section)
        {
            return ConfigurationManager.GetSection(section);
        }

        public static string GetSetting(string setting)
        {
            return ConfigurationManager.AppSettings[setting];
        }

        public static XmlDocument Load()
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(Globals.ApplicationMapPath + @"\web.config");
            return xmlDoc;
        }

        public static XmlDocument Load(string filename)
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(Globals.ApplicationMapPath + @"\" + filename);
            return xmlDoc;
        }

        public static string Save(XmlDocument xmlDoc)
        {
            string Save;
            try
            {
                XmlTextWriter writer = new XmlTextWriter(Globals.ApplicationMapPath + @"\web.config", null);
                writer.Formatting = Formatting.Indented;
                xmlDoc.WriteTo(writer);
                writer.Flush();
                writer.Close();
                Save = "";
            }
            catch (Exception exception1)
            {
                
                Exception exc = exception1;
                Save = exc.Message;
                
                return Save;
                
            }
            return Save;
        }

        public static string Save(XmlDocument xmlDoc, string filename)
        {
            string Save;
            try
            {
                XmlTextWriter writer = new XmlTextWriter(Globals.ApplicationMapPath + @"\" + filename, null);
                writer.Formatting = Formatting.Indented;
                xmlDoc.WriteTo(writer);
                writer.Flush();
                writer.Close();
                Save = "";
            }
            catch (Exception exception1)
            {
                
                Exception exc = exception1;
                Save = exc.Message;
                
                return Save;
                
            }
            return Save;
        }

        public static XmlDocument UpdateMachineKey(XmlDocument xmlConfig)
        {
            PortalSecurity objSecurity = new PortalSecurity();
            string validationKey = objSecurity.CreateKey(20);
            string decryptionKey = objSecurity.CreateKey(0x18);
            XmlNode xmlMachineKey = xmlConfig.SelectSingleNode("configuration/system.web/machineKey");
            xmlMachineKey.Attributes["validationKey"].InnerText = validationKey;
            xmlMachineKey.Attributes["decryptionKey"].InnerText = decryptionKey;
            xmlConfig = AddAppSetting(xmlConfig, "InstallationDate", DateTime.Today.ToShortDateString());
            return xmlConfig;
        }
    }
}

